package func.rank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import nodes.Node;

public class PolyRankFunction implements RankFunction<Integer> {
	
	private Integer[][] map;
	
	public PolyRankFunction(Integer[][] map) {
		this.map = map;
	}

	public void sortByRank(ArrayList<Node<Integer>> pop) {
		Comparator<Node<Integer>> comp = new 
			Comparator<Node<Integer>>() {
				public int compare(Node<Integer> n1, Node<Integer> n2) {
					return (int) (applyScoreFunction(n1) - applyScoreFunction(n2));
				}
			};
		
		Collections.sort(pop, comp);
	}
	
	public double applyScoreFunction(Node<Integer> root) {
		int dif = 0;
		for(int i = 0; i < map.length; i++) {
			Integer[] param = {map[i][0], map[i][1]};
			int v = root.evaluate(param);
			dif = dif + Math.abs(v - map[i][2]);
		}
		
		if(dif < 0)
			dif = Integer.MAX_VALUE;
				
		return dif;
	}
}
